package com.xidian.zhiancloud.service.lz.hiddeninvestigation.hiddeninvestigationbasic;

import com.xidian.zhiancloud.dao.BasicManagementMapper;
import com.xidian.zhiancloud.dao.HiddenDangerTypeBasicsMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.BasicManagement;
import com.xidian.zhiancloud.model.BasicManagementExample;
import com.xidian.zhiancloud.model.HiddenDangerTypeBasics;
import com.xidian.zhiancloud.model.HiddenDangerTypeBasicsExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InvestigationPointService {

    @Autowired
    private BasicManagementMapper basicManagementMapper;

    public List<BasicManagement> getList(int riskType, String riskPoint, int current, int size) {
        BasicManagementExample example = new BasicManagementExample();
        BasicManagementExample.Criteria criteria = example.createCriteria();
        criteria.andRiskTypeEqualTo(riskType);
        if (riskPoint != null || "".equals(riskPoint)) {
            criteria.andRiskPointLike('%' + riskPoint + '%');
        }
        return basicManagementMapper.selectByExampleWithRowbounds(example, new RowBounds(current, size));


    }

    public void insert(BasicManagement basicManagement) {
        int count = basicManagementMapper.insert(basicManagement);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void update(BasicManagement basicManagement) {
        BasicManagementExample example = new BasicManagementExample();
        BasicManagementExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(basicManagement.getId());
        criteria.andRiskTypeEqualTo(basicManagement.getRiskType());

        int count = basicManagementMapper.updateByExampleSelective(basicManagement, example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void delete(BasicManagement basicManagement) {
        BasicManagementExample example = new BasicManagementExample();
        BasicManagementExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(basicManagement.getId());
        criteria.andRiskTypeEqualTo(basicManagement.getRiskType());
        int count = basicManagementMapper.deleteByExample(example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            int result = basicManagementMapper.deleteByPrimaryKey(id);
            if (result == 0) {
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }
    }
}
